import Vue from 'vue'
import VueRouter from 'vue-router'
import System from '../views/system/System.vue'
import Register from '../views/register/Register.vue'
import Login from '../views/login/Login.vue'
import apis from '../http/apis/apis'


Vue.use(VueRouter)

const routes = [
    {
        path: '/register',
        name: 'Register',
        props: true,
        component: Register,
    },
    {
        path: '/login',
        name: 'Login',
        component: Login,
    },
    {
        path: '/',
        //重定向
        redirect: '/system'
    },
    {
        path: '/system',
        name: 'System',
        props: true,
        component: System,
        //路由独享守卫
        // beforeEnter: async (to, from, next) => {
        //     const { data } = await apis.login.isLogin();
        //     console.log(data)
        //     if (data.type == 401) {
        //         alert('你还未登录，请登录');
        //         next('/login');
        //     } else {
        //         next();
        //     }
        // },
        children: [
            {
                path: 'getLogs',
                name: 'Getlogs',
                component: () => import("../components/logs/GetLogs.vue"),
            },
            {
                path: 'adminsList',
                name: 'AdminsList',
                component: () => import("../components/Admins/AdminsList.vue"),
                meta: {
                    keepAlive: true
                }
            },
            {
                path: 'updatePassword/:_id',
                name: 'UpdatePassword',
                component: () => import("../components/Admins/UpdatePassword.vue"),
                props: true,
                meta: {
                    keepAlive: true
                }
            },
            {
                path: 'addAdmins',
                component: () => import("../components/Admins/AddAdmins.vue"),
                meta: {
                    keepAlive: true
                }
            },
            {
                path: "LessorsList",
                name: "Lessors",
                component: () => import("../components/lessors/LessorsList.vue"),
            },
            {
                path: 'getOrders',
                component: () => import('../components/orders/GetOrders.vue'),
            },
            {
                path: 'getOrdersById',
                component: () => import('../components/ordersById/GetOrdersById.vue'),
            },
            {
                path: 'addParkingsById',
                component: () => import("../components/parkingsById/AddParkingsById.vue"),
                meta: {
                    keepAlive: true
                }
            },
            {
                path:'parkingsListById',
                component:()=>import('../components/parkingsById/ParkingsListById.vue'),
                meta: {
                    keepAlive: false
                }
            },
            {
                path:'parkingsList',
                component:()=>import('../components/parkings/ParkingsList.vue'),
                

            },
            // {
            //     path: 'addTenants',
            //     component: () => import("../components/tenants/AddTenants.vue"),
            //     meta: {
            //         keepAlive: true
            //     }
            // },
            // {
            //     path: 'addTenantsById',
            //     component: () => import("../components/tenantsById/AddTenantsById.vue"),
            //     meta: {
            //         keepAlive: true
            //     }
            // },
            //         //租客列表
            {
                path: 'tenantsList',
                component: () => import("../components/tenants/TenantsList.vue"),
                meta: {
                    keepAlive: true
                }
            },
            //         //审核租客
            {
                path: 'auditTenants',
                component: () => import("../components/tenants/AuditTenants.vue"),
                meta: {
                    keepAlive: true
                }
            }
        ]
    },
]

const router = new VueRouter({
    routes,
})

// 全局守卫
router.beforeEach(async (to, from, next) => {
    const { data } = await apis.login.isLogin();
    // console.log(data)
    if (to.path == "/login" || to.path == "/register") {
        next();
    } else {
        if (data.type == 401) {
            alert('你还未登录，请登录');
            next('/login');
        } else {
            next();
        }
    }

})

export default router
